package com.syni.mdd.sjb.common.component.utils.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * @author james.xu 日期工具类-2017-06-09
 */

public class DateTimeUtils {
	public static final String MATCH_MODEL_GT = "GT";
	public static final String MATCH_MODEL_GTE = "GTE";
	public static final String MATCH_MODEL_LT = "LT";
	public static final String MATCH_MODEL_LTE = "LTE";
	public static final String DATE_PATTERN_DAY = "yyyy-MM-dd";
    public static final String DATE_PATTERN_TIMESTAMP = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_PATTERN_TIMESTAMP_T = "yyyy-MM-ddTHH:mm:ss";
    public static final String DATE_PATTERN_TIMESTAMP_01 = "yyMMddHHmmss";
	public static final String DATE_PATTERN_TIMESTAMP_03 = "yyyyMMddHHmmss";
    public static final String DATE_PATTERN_DAY_01 = "yyyyMMdd";
    public static final String DATE_PATTERN_DAY_02 = "MM/dd/yyyy";
    public static final String DATE_PATTERN_DAY_03 = "MMdd";
	public static final String DATE_PATTERN_DAY_04 = "yyMMdd";
	public static final String DATE_PATTERN_DAY_05 = "yyyy年MM月dd日";
	public static final String DATE_PATTERN_DAY_06 = "yyyy年MM月dd日 HH时mm分ss秒";
	public static final String DATE_PATTERN_DAY_07 = "yyyy.MM.dd";
	public static final String DATE_PATTERN_DAY_08 = "yyyy.MM.dd HH.mm.ss";
	public static final String DATE_PATTERN_DAY_09 = "yyyy.MM.dd";
	public static final String DATE_PATTERN_DAY_10 = "MM.dd";
	public static final String DATE_PATTERN_DAY_11 = "yyyy-MM-dd HH时";
	public static final String DATE_PATTERN_DAY_12 = "dd";
	public static final String DATE_PATTERN_DAY_13 = "yyyy年M月d日";
	public static final String DATE_PATTERN_DAY_14 = "yyyy.MM.dd HH:mm:ss";
	public static final String DATE_PATTERN_DAY_15 = "yyyy-MM-ddTHH:mm:ss.000+0000";

    public static final String DATE_PATTERN_MONTH_01 = "yyyyMM";
    public static final String DATE_PATTERN_MONTH = "yyyy-MM";
	public static final String DATE_PATTERN_MONTH_1 = "MM";
    public static final String DATE_PATTERN_TIMESTAMP_02 = "yyyy-MM-dd HH:mm";
	public static final String DATE_PATTERN_TIMESTAMP_04 = "yyyy.MM.dd HH:mm";

	public static final String ZONE_SHANGHAI = "Asia/Shanghai";


	public static SimpleDateFormat getSimpleDateFormat(String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		return sdf;
	}

	/** 日期Date转字符串 */
	public static String converDateToString(Date date, String pattern) {
		SimpleDateFormat sdf = getSimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static String converDateToString(Date date, String pattern,String zone) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		//设置为东八区
		sdf.setTimeZone(TimeZone.getTimeZone(zone));
		return sdf.format(date);

	}
	
	/** Timestamp转字符串 */
	public static String converTimestampToString(Timestamp time, String pattern) {
		SimpleDateFormat sdf = getSimpleDateFormat(pattern);
		return sdf.format(time);
	}

	/** 字符串转日期Date */
	public static Date converStringToDate(String str) {
		return converStringToDate(str, DateTimeUtils.DATE_PATTERN_DAY);
	}

	/** 字符串

	/** 字符串转日期Date */
	public static Date converLongToDate(Long longTime) {
//		SimpleDateFormat sdf = new SimpleDateFormat(Constant.DATE_PATTERN_DAY);
		if(longTime == null){
			return null;
		}
		Date date = new Date(longTime);
		return date;
	}

	public static Date converIntegerToDate(Integer timestamp) {
		Date date = new Date(timestamp * 1000L);
		return date;
	}

	/** 字符串转日期Date */
	public static Integer converDateToInterger(Date date) {
		return (int)(date.getTime() / 1000);
	}



	/** 字符串转日期Date */
	public static Date converStringToDate(String str, String pattern) {
		SimpleDateFormat sdf = getSimpleDateFormat(pattern);
		Date date = null;
		try {
			date = sdf.parse(str);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/** 字符串转Timestamp */
	public static Timestamp converStrToTimestamp(String str) {
		return Timestamp.valueOf(str);
	}

	/** 字符串转Timestamp */
	public static Integer converStrToInteger(String str) {
		return (int)(converStringToDate(str).getTime() / 1000);
	}

	public static Date converLocalDateTimeToDate(LocalDateTime localDateTime){
		if(localDateTime == null){
			return null;
		}
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
		return Date.from(zdt.toInstant());
	}

	public static Long converLocalDateTimeToLong(LocalDateTime localDateTime){
		Date date = converLocalDateTimeToDate(localDateTime);
		if(date == null){
			return null;
		}
		return date.getTime();
	}
	/** 获取月份的天数 */
	public static int getMaxDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public static Date getDayBefore(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DATE, c.get(Calendar.DATE) - 1);
		return c.getTime();
	}

	/** 获取今天初始时间 */
	public static Date getTodayStart() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		Date date = todayStart.getTime();
		return date;
	}

	/** 获取昨天初始时间 */
	public static Date getYesterdayStart() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, -24);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		Date date = todayStart.getTime();
		return date;
	}

	/** 获取当前年份 */
	public static int getCurrentYear() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		return cal.get(Calendar.YEAR);
	}

	/** 获取当前月份 */
	public static int getCurrentMonth() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		return cal.get(Calendar.MONTH) + 1;
	}

	/** 获取时间的整点 */
	public static Date getCurrHourTime(Date date){
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		date = ca.getTime();
		return date;
	}


	/** 获取年份 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	/** 获取月份 */
	public static int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH) + 1;
	}
//	/** 获取月份 */
//	public static int getMonth(Timestamp time) {
//		Calendar cal = Calendar.getInstance();
//		cal.setTime(time);
//		return cal.get(Calendar.MONTH) + 1;
//	}

	/** 获取天 */
	public static int getDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/** 获取星期 */
	public static int getDayOfWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		dayOfWeek--;
		if(dayOfWeek == 0){
			dayOfWeek = 7;
		}
		return dayOfWeek;
	}

	/** 获取今晚凌晨24:00时间*/
	/*public static int getFirstDateInDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.a(Cal);
	}*/

	/** 获取指定月份最后一天 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		cal.set(Calendar.DAY_OF_MONTH, 0);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	/** 获取指定月份第一天 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date addMonth(Date date, int months) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, months);
		return cal.getTime();
	}

	public static Date addHour(Date date, int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hour);
		return cal.getTime();
	}
	public static Integer addHour(Integer timestamp, int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(timestamp * 1000L));
		cal.add(Calendar.HOUR, hour);
		return DateTimeUtils.converDateToInterger(cal.getTime());
	}


	public static Date addMinutes(Date date, int minute) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, minute);
		return cal.getTime();
	}

	public static Integer addMinutes(Integer timestamp, int minute) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateTimeUtils.converIntegerToDate(timestamp));
		cal.add(Calendar.MINUTE, minute);
		return DateTimeUtils.converDateToInterger(cal.getTime());
	}

	public static Date addSecond(Date date, int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, second);
		return cal.getTime();
	}


	public static Date addDay(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, days);
		return cal.getTime();
	}

	public static Integer addDay(Integer timestamp, int days) {
		return DateTimeUtils.converDateToInterger(addDay(DateTimeUtils.converIntegerToDate(timestamp),days));
	}

	public static Date getLastHour(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
		return cal.getTime();
	}
	
	public static Timestamp addDay(Timestamp time, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.add(Calendar.DAY_OF_MONTH, days);
		return new Timestamp(cal.getTimeInMillis());
	}

	public static Date getStartTimeOfDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date getEndTimeOfDay(Date date) {
		date = addDay(date,1);
		return getStartTimeOfDay(date);
	}

	public static Date getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		c.setTime(getStartTimeOfDay(c.getTime()));
		return c.getTime();
	}
	
	public static Date getLastDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, 2);
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 6);
		return cal.getTime();
	}
	
	public static Timestamp getCurrentTime(){
		return new Timestamp(new Date().getTime());
	}

	public static Integer getNowTimeStamp(){
		return (int)(getCurrentTime().getTime() / 1000);
	}

    /**
     * 获取当前距离今天结束还剩多少秒
     */
    public static long getEndMill(){
        Calendar curDate = Calendar.getInstance();
        Calendar nextDayDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH), curDate.get(Calendar.DATE)+1, 0, 0, 0);
        return nextDayDate.getTimeInMillis() - curDate.getTimeInMillis();
    }

	/**
	 * 获取指定日期月份
	 */
	public static String getYearMonthStr(Date date,int i){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, i);
		Date m = c.getTime();
		SimpleDateFormat sdf = getSimpleDateFormat(DateTimeUtils.DATE_PATTERN_MONTH);
		return sdf.format(m);
	}

	/**
	 * 获取指定日期月份
	 */
	public static Date getYearMonth(Date date,int i){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, i);
		Date m = c.getTime();
		return m;
	}

	/**
	 * 获取指定日期得时间天
	 * @param
	 */
	public static String getMonthDayStr(Date date,int i){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, i);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		Date m = cal.getTime();
		SimpleDateFormat sdf = getSimpleDateFormat(DateTimeUtils.DATE_PATTERN_DAY);
		return sdf.format(m);
	}

	public static boolean match(Date time1,Date time2,String model){
		Integer startTimes = converDateToInterger(time1);
		Integer endTimes = converDateToInterger(time2);
		if(MATCH_MODEL_GT.equals(model)){
			return startTimes > endTimes;
		}else if(MATCH_MODEL_GTE.equals(model)){
			return startTimes >= endTimes;
		}else if(MATCH_MODEL_LT.equals(model)){
			return startTimes < endTimes;
		}else if(MATCH_MODEL_LTE.equals(model)){
			return startTimes <= endTimes;
		}
		return false;
	}

	public static void main(String[] args){
		dos();
	}


	public static void dos(){
		Date date = new Date();
		System.out.println(addMonth(date,6).getTime());

	}

	public static LocalDateTime converDateToLocalDateTime(Date date) {
		if(date == null){
			return null;
		}
		Instant instant = Instant.ofEpochMilli(date.getTime());
		ZoneId zone = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(instant, zone);
	}

	public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
		Instant instant = Instant.ofEpochMilli(timestamp);
		ZoneId zone = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(instant, zone);
	}

	public static Integer getDate(Date nowTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(nowTime);
		return cal.get(Calendar.DATE);
	}

	public static Integer getThisMonthDayNum(Date nowTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(nowTime);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public static Integer getThisMonthDayNum(Integer timestamp) {
		return getThisMonthDayNum(DateTimeUtils.converIntegerToDate(timestamp));
	}

    public static Integer getStartHour(Integer timestamp) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(timestamp * 1000L));
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return DateTimeUtils.converDateToInterger(cal.getTime());
    }

    public static Integer getHour(Integer timestamp) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(converIntegerToDate(timestamp));
		return cal.get(Calendar.HOUR_OF_DAY);
    }

    public static int getWeek(Date time) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(w == 0){
			w = 7;
		}
		return w;
    }


	public static String converDateStringToString(String changeTime, String datePatternTimestamp) {
		try {
			String[] changeTimes =changeTime.split("\\.000+");
			String[] times = changeTimes[0].split("T");
			Date date = DateTimeUtils.converStringToDate(times[0] + " " + times[1],DATE_PATTERN_TIMESTAMP);
			return DateTimeUtils.converDateToString(date,datePatternTimestamp);
		}catch (Exception e){}
		return "";
	}
}
